package edu.columbia.cloudbox;

import java.io.IOException;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import edu.columbia.cloudbox.io.LocalIO;
import edu.columbia.cloudbox.io.S3IO;
import edu.columbia.cloudbox.io.S3ObjectNotFoundException;
import edu.columbia.cloudbox.policy.CBDBHelper;
import edu.columbia.cloudbox.policy.Policy.AutoPolicy;
import edu.columbia.cloudbox.policy.UserPreference;

public class UserStatusService extends Service {

	private static String className = UserStatusService.class.getSimpleName();

	public static boolean STARTED = false;

	public static boolean USER_LOGGED_IN = false;
	public static String USER_NAME = null;
	public static S3IO S3IO_INSTANCE = null;
	public static LocalIO LocalIO_INSTANCE = null;
	public static UserPreference user_preference = null;

	private static Context context;

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		final String methodName = "onCreate()";
		Log.i(className, methodName + ": Creating...");
	}

	@Override
	public void onStart(Intent intent, int startId) {
		Log.i(className, "Starting...");
		STARTED = true;
	}

	public static void initialize(Context c) {
		String methodName = "initialize()";
		Log.i(className, methodName + "Loading user preference...");

		context = c;

		user_preference = UserPreference.getInstance();
		user_preference.loadPreference();
		Log.i(className, methodName + "isManual: " + UserPreference.isManual);
		Log.i(className, methodName + "user extensions: "
				+ UserPreference.userExts);

		Log.i(className, methodName + ": " + UserPreference.userInterval);

		if (UserPreference.isManual) {
			cacheFilesWithUserExts();
		} else {
			cacheFilesWithUserPolicy();
		}

	}

	public static void cacheFilesWithUserExts() {
		final String className = "cacheFilesWithUserExts() ";
		new Thread(new Runnable() {
			@Override
			public void run() {
				Log.i(className, className
						+ ": download cache files in service.");
				List<String> keys = S3IO_INSTANCE.getKeysByExts(USER_NAME,
						UserPreference.userExts);
				for (String key : keys) {
					try {
						Log.i(className, className + ": downloading " + key
								+ " to " + USER_NAME + "_cache/" + key);

						if (LocalIO_INSTANCE.doesFileExist(key)) {
							Log.i(className, className + ": File exists!");
						} else {
							S3IO_INSTANCE.downloadTo(key, USER_NAME + "_cache/"
									+ key);
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (S3ObjectNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}).start();
	}

	public static void cacheFilesWithUserPolicy() {
		final String methodName = "cacheFilesWithUserPolicy()";
		new Thread(new Runnable() {
			@Override
			public void run() {
				Log.i(className, className + ": download cache files in policy");

				List<String> keys = new Vector<String>();

				CBDBHelper cbdhelper = new CBDBHelper(context);
				int userPolicy = UserPreference.userPolicy;
				if (AutoPolicy.values()[userPolicy] == AutoPolicy.LARGE_SIZE_FIRST) {
					keys = cbdhelper.querySizeFirst(5);
				} else if (AutoPolicy.values()[userPolicy] == AutoPolicy.RECENT_ACCESSED_FIRST) {
					keys = cbdhelper.queryMostRecent(5);
				} else if (AutoPolicy.values()[userPolicy] == AutoPolicy.TOTAL_ACCESSED_FIRST) {
					keys = cbdhelper.queryMostAccessed(5);
				}

				Log.i(className, methodName + ": Keys: " + keys);
				cbdhelper.close();

				for (String key : keys) {
					try {
						Log.i(className, className + ": downloading " + key
								+ " to " + USER_NAME + "_cache/" + key);

						if (LocalIO_INSTANCE.doesFileExist(key)) {
							Log.i(className, className + ": File exists!");
						} else {
							S3IO_INSTANCE.downloadTo(key, USER_NAME + "_cache/"
									+ key);
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (S3ObjectNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}).start();
	}
}
